#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "app_ir.h"
#include "app_beep.h"
// #include "asr_config.h"

static void asr_other_actiont(unsigned int i);
static void asr_air_actiont(unsigned int i);
static void asr_fan_actiont(unsigned int i);

extern volatile unsigned char f_asr_response;
extern unsigned char f_asr_wakeup;
extern volatile unsigned int asr_wakeup_tick_timer;
extern unsigned char *remote_ptr_data;

#ifdef STUDY_FAN_CODE
extern unsigned char learn_fan_remote_flag;
#endif

#ifdef KUKONG_FAN_LIB
extern unsigned char match_fan_remote_flag;
#endif
static unsigned char asr_result[ASR_RESPONSE_RESULT_INDEX][19] = {
    ///*00*/ {"小智同学"},
    /*00*/ {"语音宝贝"},
    /*01*/ {"语音导航"},

    /*02*/ {"匹配空调"},
    /*36*/ {"匹配电风扇"},
    /*37*/ {"匹配风扇"},
    /*63*/ {"学习风扇"},

    //以下为空调的控制指令
    /*03*/ {"打开空调"},
    /*04*/ {"关闭空调"},
    /*05*/ {"制冷模式"},
    /*06*/ {"制热模式"},
    /*07*/ {"加热模式"},

    /*08*/ {"抽湿模式"},
    /*09*/ {"换气模式"},
    /*10*/ {"送风模式"},
    /*11*/ {"调高温度"},
    /*12*/ {"升高温度"},
    /*13*/ {"调低温度"},
    /*14*/ {"降低温度"},
    /*15*/ {"十六度"},
    /*16*/ {"十七度"},
    /*17*/ {"十八度"},
    /*18*/ {"十九度"},
    /*19*/ {"二十度"},
    /*20*/ {"二十一度"},
    /*21*/ {"二十二度"},
    /*22*/ {"二十三度"},
    /*23*/ {"二十四度"},
    /*24*/ {"二十五度"},
    /*25*/ {"二十六度"},
    /*26*/ {"二十七度"},
    /*27*/ {"二十八度"},
    /*28*/ {"二十九度"},
    /*29*/ {"三十度"},
    /*30*/ {"最大风量"},
    /*31*/ {"最小风量"},
    /*32*/ {"中等风量"},
    /*33*/ {"自动风量"},
    /*34*/ {"上下摆风"},
    /*35*/ {"停止摆风"},

    //以下为风扇的控制指令
    /*38*/ {"打开电风扇"},
    /*39*/ {"关闭电风扇"},
    /*40*/ {"打开风扇"},
    /*41*/ {"关闭风扇"},
    /*42*/ {"摇头功能"},
    /*43*/ {"开始摇头"},
    /*44*/ {"停止摇头"},
    /*45*/ {"上下摇头"},
    /*46*/ {"左右摇头"},
    /*47*/ {"变速功能"},
    /*48*/ {"调大风速"},
    /*49*/ {"调小风速"},
    /*50*/ {"定时功能"},
    /*51*/ {"增加定时"},
    /*52*/ {"减小定时"},
    /*53*/ {"净化功能"},
    /*54*/ {"开始净化"},
    /*55*/ {"停止净化"},
    /*56*/ {"切换模式"},
    /*57*/ {"AI模式"},

    /*58*/ {"打开语音"},
    /*59*/ {"关闭语音"},
    /*60*/ {"声音大一点"},
    /*61*/ {"声音小一点"},

    /*62*/ {"切换码库"},

};

int asr_ir_deal(int item)
{
/*
    if (!asrData)
    {
        return 0;
    }

    int ret = 0;
    unsigned int item = 0;

    for (; item < ASR_RESPONSE_RESULT_INDEX; item++)
    {
        if (strcmp(asrData, (const char *)&asr_result[item]) == 0)
        {
            break;
        }
    }*/
	int ret = 0;
    if (item >= ASR_RESPONSE_RESULT_INDEX)
        return 0;

    printf("Asr ID: %d ;", item);

    if (item >= ASR_DA_KAI_KONG_TIAO && item <= ASR_TING_ZHI_BAI_FENG)
    {

        if (get_remote_flag(IR_DEV_AIR_COND) == 0)
        {
            printf("\nPlease match the remote control first\n");
            f_asr_response = BEEP_0087;
        }
        else
        {
            ret = read_remote_code(IR_DEV_AIR_COND);
            if (ret == -1)
            {
                printf("\nFail to read data, error code = %d!\n", ret);
                return -1;
            }
            else
            {
                asr_air_actiont(item);
            }

            free_remote_code();
        }
    }
#if 0
    else if (item >= ASR_DA_KAI_DIAN_FENG_SAN && item <= ASR_AI_MO_SHI)
    {
#ifdef KUKONG_FAN_LIB
        if (get_remote_flag(IR_DEV_FAN_DEVICE) == 0)
        {
            printf("\nPlease match the remote control first\n");
           //f_asr_response = BEEP_0087;
           //请先匹配风扇遥控器
        }
        else
        {
            ret = read_remote_code(IR_DEV_FAN_DEVICE);
            if (ret == -1)
            {
                printf("\nFail to read data, error code = %d!\n", ret);
                return -1;
            }
            else
            {
                asr_air_actiont(item);
            }

            free_remote_code();
        }
#else
        asr_fan_actiont(item);
#endif
    }
    else
    {
        asr_other_actiont(item);
    }
#endif

    printf("ReadmePlay ID: %d\n", f_asr_response);
    return 0;
}
#if 0
static void asr_other_actiont(unsigned int i)
{
    int ret = 0;
    switch (i)
    {

    case ASR_XIAO_ZHI:
    case ASR_YU_YIN_DAO_HANG: //{"语音导航"},
        f_asr_wakeup = 1;
        asr_wakeup_tick_timer = 0; //唤醒定时退出
        f_asr_response = BEEP_0002;
        break;

    case ASR_DA_KAI_YU_YIN: //{"打开语音"},

        //f_asr_response = BEEP_0072;
        break;
    case ASR_GUAN_BI_YU_YIN: //{"关闭语音"},
        f_asr_response = BEEP_0073;
        f_asr_wakeup = 0;
        break;

    case ASR_VOL_UP:
        ret = app_vol_up();
        if (ret < 0)
        {
            f_asr_response = BEEP_0126;
        }
        else
        {
            f_asr_response = BEEP_0124;
        }
        break;
    case ASR_VOL_DOWN:
        ret = app_vol_down();
        if (ret < 0)
        {
            f_asr_response = BEEP_0127;
        }
        else
        {
            f_asr_response = BEEP_0125;
        }
        break;

    case ASR_QIE_HUAN_KONG_TIAO_MA_KU:
        ret = event_next_air_remote();
        if (ret < 0)
        {
            f_asr_response = BEEP_0120;
        }
        else
        {
            f_asr_response = BEEP_0119;
        }

        break;

    case ASR_PI_PEI_KONG_TIAO: //{"匹配空调"},
        match_remote_flag = 1;
		while(match_remote_process() == 0)
		{
			;
		}
        break;

    case ASR_XUE_XI_FENG_SAN:

#ifdef STUDY_FAN_CODE
        learn_fan_remote_flag = 1;
#endif
        break;
    case ASR_PI_PEI_FENG_SAN:

#ifdef KUKONG_FAN_LIB
        match_fan_remote_flag = 1;
#endif
        break;

    default:
#if APP_LOG_ENABLE
        printf("None...\n");
#endif
        break;
    }
}

static void asr_fan_actiont(unsigned int i)
{
    int ret = 0;

    switch (i)
    {
    //case ASR_DA_KAI_DIAN_FENG_SAN: //{"打开电风扇"},
    case ASR_DA_KAI_FENG_SAN: //{"打开风扇"},
        ret = fan_send_buf(UASR_FAN_POWER_ON);
        if (ret < 0)
        { //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            //f_asr_response = BEEP_0057;
            f_asr_response = BEEP_0082;
        }
        break;
    //case ASR_GUAN_BI_DIAN_FENG_SAN: //{"关闭电风扇"},
    case ASR_GUAN_BI_FENG_SAN: //{"关闭风扇"},
        ret = fan_send_buf(UASR_FAN_POWER_OFF);
        if (ret < 0)
        { //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            //f_asr_response = BEEP_0058;
            f_asr_response = BEEP_0083;
        }
        break;

    case ASR_SHANG_XIA_YAO_TOU:
        ret = fan_send_buf(UASR_FAN_SHAKE_UP_DOWN);
        if (ret < 0)
        {
            //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0112;
        }
        break;
    case ASR_ZUO_YOU_YAO_TOU:
        ret = fan_send_buf(UASR_FAN_SHAKE_LEFT_RIGHT);
        if (ret < 0)
        {
            //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0113;
        }
        break;

    case ASR_TIAO_DA_FENG_SU:
        ret = fan_send_buf(UASR_FAN_SPEED_UP);
        if (ret < 0)
        {
            //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0108;
        }
        break;
    case ASR_TIAO_XIAO_FENG_SU:
        ret = fan_send_buf(UASR_FAN_SPEED_DOWN);
        if (ret < 0)
        {
            //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0109;
        }
        break;

    case ASR_ZENG_JIA_DING_SHI:
        ret = fan_send_buf(UASR_FAN_TIMER_UP);
        if (ret < 0)
        {
            //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0110;
        }

        break;
    case ASR_JIAN_XIAO_DING_SHI:
        ret = fan_send_buf(UASR_FAN_TIMER_DOWN);
        if (ret < 0)
        {
            //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0111;
        }

        break;

    case ASR_KAI_SHI_JING_HUA:
        ret = fan_send_buf(UASR_FAN_PURIFY_ON);
        if (ret < 0)
        { //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0115;
        }
        break;
    case ASR_TING_ZHI_JING_HUA:
        ret = fan_send_buf(UASR_FAN_PURIFY_OFF);
        if (ret < 0)
        { //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0116;
        }
        break;

    case ASR_QIE_HUAN_MO_SHI:
        ret = fan_send_buf(UASR_FAN_MODE);
        if (ret < 0)
        {
            //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0062;
        }
        break;
    case ASR_AI_MO_SHI:
        ret = fan_send_buf(UASR_FAN_AI_MODE);
        if (ret < 0)
        {
            //请先匹配遥控器或学习该功能红外码
#ifdef STUDY_FAN_CODE
            f_asr_response = BEEP_0101;
#endif
        }
        else
        {
            f_asr_response = BEEP_0118;
        }
        break;

    default:
#if APP_LOG_ENABLE
        printf("None...\n");
#endif
        break;
    }
}
#endif
static void asr_air_actiont(unsigned int i)
{
    int ret = 0;

    switch (i)
    {

    case ASR_DA_KAI_KONG_TIAO: //{"打开空调"},
        event_air_power_on();
        f_asr_response = BEEP_0016;
        break;
    case ASR_GUAN_BI_KONG_TIAO: //{"关闭空调"},
        event_air_power_off();
        f_asr_response = BEEP_0017;
        break;
    case ASR_ZHI_LENG_MO_SHI: //{"制冷模式"},
        ret = event_air_mode_cool();
        if (ret < 0)
        { //不支持的模式
            f_asr_response = BEEP_0107;
        }
        else
        {
            f_asr_response = BEEP_0018;
        }

        break;

    case ASR_JIA_RE_MO_SHI: //{"加热模式"},
        ret = event_air_mode_hot();
        if (ret < 0)
        { //不支持的模式
            f_asr_response = BEEP_0107;
        }
        else
        {
            f_asr_response = BEEP_0020;
        }
        break;

    case ASR_CHOU_SHI_MO_SHI: //{"抽湿模式"},
        ret = event_air_mode_wetted();
        if (ret < 0)
        { //不支持的模式
            f_asr_response = BEEP_0107;
        }
        else
        {
            f_asr_response = BEEP_0021;
        }
        break;
    //case ASR_HUAN_QI_MO_SHI:   //{"换气模式"},
    case ASR_SONG_FENG_MO_SHI: //{"送风模式"},
        ret = event_air_mode_wind();
        if (ret < 0)
        { //不支持的模式
            f_asr_response = BEEP_0107;
        }
        else
        {
            //f_asr_response = BEEP_0022;
            f_asr_response = BEEP_0023;
        }
        break;

    case ASR_TIAO_GAO_WEN_DU: //{"调高温度"},
                              //case ASR_SHENG_GAO_WEN_DU: //{"升高温度"},

        ret = event_air_temperature_increase();
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0024;
            //f_asr_response = BEEP_0080;
        }
        break;

    case ASR_TIAO_DI_WEN_DU: //{"调低温度"},
                             //case ASR_JIANG_DI_WEN_DU: //{"降低温度"},
        ret = event_air_temperature_reduce();
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0025;
            //f_asr_response = BEEP_0081;
        }
        break;

    case ASR_16_DU: //{"十六度"},
        ret = event_air_temperature_set(16);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0026;
        }
        break;
    case ASR_17_DU: //{"十七度"},
        ret = event_air_temperature_set(17);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0027;
        }
        break;
    case ASR_18_DU: //{"十八度"},
        ret = event_air_temperature_set(18);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0028;
        }
        break;
    case ASR_19_DU: //{"十九度"},
        ret = event_air_temperature_set(19);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0029;
        }
        break;
    case ASR_20_DU: //{"二十度"},
        ret = event_air_temperature_set(20);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0030;
        }
        break;
    case ASR_21_DU: //{"二十一度"},
        ret = event_air_temperature_set(21);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0031;
        }
        break;
    case ASR_22_DU: //{"二十二度"},
        ret = event_air_temperature_set(22);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0032;
        }
        break;
    case ASR_23_DU: //{"二十三度"},
        ret = event_air_temperature_set(23);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0033;
        }
        break;
    case ASR_24_DU: //{"二十四度"},
        ret = event_air_temperature_set(24);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0034;
        }
        break;
    case ASR_25_DU: //{"二十五度"},
        ret = event_air_temperature_set(25);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0035;
        }
        break;
    case ASR_26_DU: //{"二十六度"},
        ret = event_air_temperature_set(26);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0036;
        }
        break;
    case ASR_27_DU: //{"二十七度"},
        ret = event_air_temperature_set(27);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0037;
        }
        break;
    case ASR_28_DU: //{"二十八度"},
        ret = event_air_temperature_set(28);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0038;
        }
        break;
    case ASR_29_DU: //{"二十九度"},
        ret = event_air_temperature_set(29);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0039;
        }
        break;
    case ASR_30_DU: //{"三十度"},
        ret = event_air_temperature_set(30);
        if (ret < 0)
        {
            f_asr_response = BEEP_0102;
        }
        else
        {
            f_asr_response = BEEP_0040;
        }
        break;

    case ASR_ZUI_DA_FENG_LIANG: //{"最大风量"},
        ret = event_air_wind_speed_max();
        if (ret < 0)
        {
            f_asr_response = BEEP_0104;
        }
        else
        {
            f_asr_response = BEEP_0043;
        }
        break;
    case ASR_ZUI_XIAO_FENG_LIANG: //{"最小风量"},
        ret = event_air_wind_speed_min();
        if (ret < 0)
        {
            f_asr_response = BEEP_0104;
        }
        else
        {
            f_asr_response = BEEP_0044;
        }
        break;
    case ASR_ZHONG_DENG_FENG_LIANG: //{"中等风量"},
        ret = event_air_wind_speed_mid();
        if (ret < 0)
        {
            f_asr_response = BEEP_0104;
        }
        else
        {
            f_asr_response = BEEP_0045;
        }
        break;
    case ASR_ZI_DONG_FENG_LIANG: //{"自动风量"},
        ret = event_air_wind_speed_auto();
        if (ret < 0)
        {
            f_asr_response = BEEP_0104;
        }
        else
        {
            f_asr_response = BEEP_0046;
        }
        break;
    case ASR_SHANG_XIA_BAI_FENG: //{"上下摆风"},
        ret = event_air_mode_direction();
        if (ret < 0)
        {
            f_asr_response = BEEP_0106;
        }
        else
        {

            f_asr_response = BEEP_0047;
        }

        break;
    case ASR_TING_ZHI_BAI_FENG: //{"停止摆风"},
        ret = event_air_mode_stop_direction();
        if (ret < 0)
        {
            f_asr_response = BEEP_0106;
        }
        else
        {

            f_asr_response = BEEP_0049;
        }

    default:
#if APP_LOG_ENABLE
        printf("None...\n");
#endif
        break;
    }
}

int asr_set_temparature(unsigned char temp)
{
    int ret;
    if (get_remote_flag(IR_DEV_AIR_COND) == 0)
    {
        printf("\nPlease match the remote control first\n");
        f_asr_response = BEEP_0087;
    }
    else
    {
        ret = read_remote_code(IR_DEV_AIR_COND);
        if (ret == -1)
        {
            printf("\nFail to read data, error code = %d!\n", ret);
            return -1;
        }
        else
        {
            ret = event_air_temperature_set(temp);
            if (ret < 0)
            {
                f_asr_response = BEEP_0102;
            }
            else
            {
                f_asr_response = temp + (BEEP_0030 - 20);
            }
        }

        free_remote_code();
    }
}